package com.szewec.ps.ui.road.facilities.traffic;

import androidx.activity.result.contract.ActivityResultContracts;
import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import androidx.viewpager.widget.PagerAdapter;

import android.content.Intent;
import android.graphics.Point;
import android.graphics.Typeface;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.szewec.ps.R;
import com.szewec.ps.base.Constants;
import com.szewec.ps.base.Enums;
import com.szewec.ps.databinding.ActivityRoadIntersectionInvestigationTaskBinding;
import com.szewec.ps.model.MyPoint;
import com.szewec.ps.model.PresentValue;
import com.szewec.ps.model.ViewModeValue;
import com.szewec.ps.model.road.traffic.RoadIntersectionInvestigationTask;
import com.szewec.ps.model.road.traffic.RoadIntersectionTrafficCount;
import com.szewec.ps.model.road.traffic.RoadTrafficCount;
import com.szewec.ps.model.task.SurveyObjectModel;
import com.szewec.ps.myview.InputItem;
import com.szewec.ps.myview.indicator.IndicatorViewPager;
import com.szewec.ps.myview.indicator.slidebar.DrawableBar;
import com.szewec.ps.ui.common.SelectCoordinatesActivity;
import com.szewec.ps.ui.road.facilities.intersection.task.VehicleLaneActivity;
import com.szewec.ps.ui.road.facilities.intersection.task.constants.Consts;
import com.szewec.ps.ui.road.facilities.intersection.task.utils.RollUtils;
import com.szewec.ps.ui.road.facilities.intersection.task.utils.ViewUtils;
import com.szewec.ps.util.CommonUtils;
import com.szewec.ps.util.ListUtil;
import com.szewec.ps.util.RollUtil;
import com.szewec.ps.util.TextViewUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.ButterKnife;
import butterknife.OnClick;

public class RoadIntersectionInvestigationTaskActivity extends AppCompatActivity {

    private ActivityRoadIntersectionInvestigationTaskBinding binding;

    // 路口类型
    private List<String> intersectionTypeDict = new ArrayList<>();
    // 管控方式
    private List<String> controlModeDict = new ArrayList<>();
    // 模式
    private List<String> patternDict = new ArrayList<>();

    // 用于区分新增还是修改还是详情展示
    private String modeFlag;
    private ViewModeValue viewModeValue;
    // 路口交通量调查任务实体类
    private RoadIntersectionInvestigationTask investigationTask;
    // 调查对象
    private SurveyObjectModel surveyObjectModel;
    // 坐标
    private List<MyPoint> pointList;

    // 车流计数适配器相关
    private Point screenPoint;
    private int lineNum = 4;
    private IndicatorViewPager indicatorViewPager1;
    private List<String> tabs = new ArrayList<>();
    private DrawableBar drawableBar;
    private List<Integer> textWidths = new ArrayList<>();
    private List<RoadIntersectionTrafficCount> intersectionTrafficCountList = new ArrayList<>();
//    private MyAdapter1 myAdapter;
    protected Map<Integer, View> myAdapterViews = new HashMap<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_road_intersection_investigation_task);
        ButterKnife.bind(this);

//        initData();
//        initTitleBar();
    }

    private void initData() {
        // 初始化字典
        intersectionTypeDict = Enums.IntersectionTypeEnum.getEnumValueList();
        controlModeDict = Enums.ControlModeEnum.getEnumValueList();
        patternDict = Enums.TrafficPatternTypeEnum.getEnumValueList();

        // 获取intent中的数据
        PresentValue<SurveyObjectModel> presentValue = (PresentValue<SurveyObjectModel>) getIntent().getSerializableExtra(Constants.EXTRA_KEY.ROAD_TRAFFIC_INVESTIGATION_TASK);
        //获取是新增还是修改还是详情展示
        assert presentValue != null;
        modeFlag = presentValue.getFlag();
        // 获取调查对象
        surveyObjectModel = presentValue.getT();
        Log.i("路口交通量调查 调查对象", JSONObject.toJSONString(surveyObjectModel));
    }

    private void initTitleBar() {
        viewModeValue = new ViewModeValue(this, modeFlag, binding.titleBar, binding.llContainer);

        //设置导航栏右侧按钮
        if (Constants.PRESENT_VALUE_FLAG.VIEW.equals(modeFlag)) {
            CommonUtils.setViewMode(viewModeValue, this::finish, this::callMoreMenu);
        } else if (Constants.PRESENT_VALUE_FLAG.ADD.equals(modeFlag)
                || Constants.PRESENT_VALUE_FLAG.EDIT.equals(modeFlag)) {
            CommonUtils.setViewMode(viewModeValue, this::finish, this::appendOrModifyData);
        }
    }

    private void callMoreMenu() {
        CommonUtils.showMoreMenu(this, binding.titleBar.getRightLayout(), this::linkToEditActivity, this::deleteData);
    }

    private void deleteData() {

    }

    private void linkToEditActivity() {
    }

    private void appendOrModifyData() {

    }

    @OnClick({R.id.intersectionType, R.id.controlMode, R.id.pattern, R.id.locationEdit})
    public void onViewClicked(View view) {
        //隐藏系统软键盘
        TextViewUtil.closeKeyboard(this);
        switch (view.getId()) {
            case R.id.intersectionType:
                RollUtil.alertBottomWheelOption(this, intersectionTypeDict, binding.intersectionType.getSelectTextView(), new RollUtil.OnWheelViewClick() {
                    @Override
                    public void onClick(View view, int index) {
                        binding.intersectionType.setValue(intersectionTypeDict.get(index));
                    }
                });
                break;
            case R.id.controlMode:
                RollUtil.alertBottomWheelOption(this, controlModeDict, binding.controlMode.getSelectTextView(), new RollUtil.OnWheelViewClick() {
                    @Override
                    public void onClick(View view, int index) {
                        binding.controlMode.setValue(controlModeDict.get(index));
                    }
                });
                break;
            case R.id.pattern:
                RollUtil.alertBottomWheelOption(this, patternDict, binding.pattern.getSelectTextView(), new RollUtil.OnWheelViewClick() {
                    @Override
                    public void onClick(View view, int index) {
                        binding.pattern.setValue(patternDict.get(index));
                    }
                });
                break;
            // 点击坐标
            case R.id.locationEdit:
                String point = null;
                Intent intent = new Intent(this, SelectCoordinatesActivity.class);
                if (ListUtil.isNotEmpty(pointList)) {
                    point = JSONArray.toJSONString(pointList);
                    binding.locationEdit.setText("查看路段");
                }
                PresentValue<String> presentValue = new PresentValue<>(Constants.SELECT_COOR_FLAG.SELECT_COOR, point);
                intent.putExtra(Constants.EXTRA_KEY.SELECT_COOR, presentValue);
                registerForActivityResult(new ActivityResultContracts.StartActivityForResult(), v -> {
                    if (v.getData() != null) {
                        pointList = JSONObject.parseArray((v.getData().getStringExtra(Constants.EXTRA_KEY.SELECT_COOR)), MyPoint.class);
                    }
                }).launch(intent);
                break;
        }
    }

    protected void setImg(String condition, ImageView imageView) {
        switch (condition) {
            case Consts.LaneDirection.LEFT_TURN:
                imageView.setImageResource(R.mipmap.ic_left_turn);
                break;
            case Consts.LaneDirection.STRAIGHT_LEFT:
                imageView.setImageResource(R.mipmap.ic_straight_left);
                break;
            case Consts.LaneDirection.STRAIGHT:
                imageView.setImageResource(R.mipmap.ic_straight);
                break;
            case Consts.LaneDirection.STRAIGHT_RIGHT:
                imageView.setImageResource(R.mipmap.ic_straight_right);
                break;
            case Consts.LaneDirection.RIGHT_TURN:
                imageView.setImageResource(R.mipmap.ic_right_turn);
                break;
            case Consts.LaneDirection.TURN_AROUND:
                imageView.setImageResource(R.mipmap.ic_turn_around);
                break;
        }
    }

    /**
     * 适配器
     */
//    public class MyAdapter1 extends IndicatorViewPager.IndicatorViewPagerAdapter {
//        protected List<TextView> tabViews = new ArrayList<>();
//
//        @Override
//        public int getCount() {
//            return tabs.size();
//        }
//
//        @Override
//        public View getViewForTab(int position, View convertView, ViewGroup container) {
//            if (convertView == null) {
//                convertView = getLayoutInflater().inflate(R.layout.tab_top, container, false);
//            }
//            TextView textView = (TextView) convertView;
//            textView.setText(tabs.get(position));
//            //因为wrap的布局 字体大小变化会导致textView大小变化产生抖动，这里通过设置textView宽度就避免抖动现象
//            //1.3f是根据上面字体大小变化的倍数1.3f设置
//            textView.setWidth(screenPoint.x / lineNum);
//            textView.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
//            int textWidth = ViewUtils.getTextWidth(textView);
//            textWidths.add(textWidth);
//            tabViews.add(textView);
//            return textView;
//        }
//
//        @Override
//        public View getViewForPage(int position, View convertView, ViewGroup container) {
//            if (convertView == null) {
//                convertView = getLayoutInflater().inflate(R.layout.page_traffic_flow_count, container, false);
//            }
//            ImageView imageView = convertView.findViewById(R.id.iv_guide_arrow);
//            setImg(tabs.get(position), imageView);
//
//            myAdapterViews.put(position, convertView);
//
//            return convertView;
//        }
//
//        @Override
//        public int getItemPosition(Object object) {
//            //这是ViewPager适配器的特点,有两个值 POSITION_NONE，POSITION_UNCHANGED，默认就是POSITION_UNCHANGED,
//            // 表示数据没变化不用更新.notifyDataChange的时候重新调用getViewForPage
//            return PagerAdapter.POSITION_UNCHANGED;
//        }
//
//        @Override
//        public void notifyDataSetChanged() {
//            this.tabViews.clear();
//            super.notifyDataSetChanged();
//        }
//    }

}